<?php
namespace app\admin\model;

use think\Model;
class Term extends Model
{


	public function selectAll()
	{
		$select_all = $this->select();
		return $select_all;
	}
    /**
     * 获取某个栏目的父栏目id，暂时无用
	public function belongtoTerm($id)
	{
		$belongto_term = $this->find($id);
		$belong_parent_id = $belongto_term['parent_id'];
		return $belong_parent_id;
	}
     */
	 
	 public function listorder($ids=0)
	 {
		  $pk = $this->getPk(); //获取主键名称
		  foreach ($ids as $key => $r) {
            $data['listorder'] = $r;
            $this->where(array($pk => $key))->save($data);
        }
		 return true;
	 }
	 
	
	public function addTerm()
	{
		
	  $data = array(
		'model_id'=>I('post.model_id'),
		'parent_id'=>I('post.parent_id'),
		'flag'=>I('post.flag'),
		'name'=>I('post.name'),
		'description'=>I('post.description'),
		'display_nav'=>I('post.display_nav'),
		'seo_title'=>I('post.seo_title'),
		'seo_keywords'=>I('post.seo_keywords'),
		'seo_description'=>I('post.seo_description'),
		'tags_value'=>json_encode([]),
		);
		 $add_term = $this->add($data);
		 return $add_term;
	}
	

    /**
     * 重构editTerm方法
     * 马宇翔
     */
	public function getTermInfo($id)
	{
        if(empty($id))
            return false;
	    return $this->find($id);
    }
	
	public function postEdit($tagsValue){
		$data = array(
		'id'=>I('post.id'),
		'flag'=>I('post.flag'),
		'display_nav'=>I('post.display_nav'),
		'parent_id'=>I('post.parent_id'),
		'name'=>I('post.name'),
		'description'=>I('post.description'),
		'status'=>I('post.status'),
		'seo_title'=>I('post.seo_title'),
		'seo_keywords'=>I('post.seo_keywords'),
		'seo_description'=>I('post.seo_description'),
		'tags_value'=>$tagsValue,
		);
		$post_edit = $this->save($data);
	}
    
	public function delTerm($id){
		$del_term =$this->delete($id);
		return $del_term;
	}
    

	
	public function displayNav($displaynav,$navid)
	{
		$data['display_nav']=$displaynav;
		$display_nav = $this->where('id='.$navid)->save($data);
	}
    public function getList($options = array()){
        if(is_numeric($options)){
            $parent_id 	= $options;
            $options 	= array();
        }else{
            $options["parent_id"] = isset($options["parent_id"]) 	? $options["parent_id"] : null;
            $parent_id            = is_null($options["parent_id"]) 	? $this->parent_id 	: $options["parent_id"];
        }
        return $this->where(array("parent_id"=>$parent_id))->order('listorder desc')->select($options);
    }

	public function hasChild($options){
		$parent_id = $options;
        $rs = $this->where(["parent_id"=>$parent_id])->order('listorder desc')->select();
		return empty($rs) ? false : $rs;
	}

	public function getChilds($options){
		return $this->_getChilds($options);
	}

	public $childNode = array();

	private function _getChilds($id){
		$child = $this->where(["parent_id"=>$id])->order('listorder desc')->select();
		if($child){
			foreach ($child as $key => $value) {
				$this->childNode[] = $value["id"];
				$this->_getChilds($value["id"]);
			}
		}
		return $this->childNode;
	}
    public function depth($options){
        return $this->_depth($options);
    }
    public $depth = ['0'=>0];

    private function _depth($parent_id){
        $rs = D("Term")->where(['id'=>$parent_id])->find();
        array_push($this->depth, $parent_id);
        if($rs['parent_id'] != 0){
            $this->_depth($rs['parent_id']);
        }
        return $this->depth;
    }
}
